﻿using Devil;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(InputController), true)]
    public class InputControllerEditor : UnityEditor.Editor
    {
        float height = 20;
        InputController.KeyInfo[] mKeys;
        string[] keynames = new string[2];

        bool mDebugKey;
        string mDebugKeyName = "";
        InputController.IInputAction mAction;

        private void OnEnable()
        {
            ((InputController)target).Prepare(null);
            StringBuilder[] bufs = { ParallelUtils.GetBuilder(), ParallelUtils.GetBuilder() };
            //buf.Append("<<INCLUDE KEYS>>");
            mKeys = ((InputController)target).GetKeysInfo();
            var len = mKeys.Length / 2;
            if ((mKeys.Length & 1) == 1)
                len++;
            for (int i = 0; i < mKeys.Length; i++)
            {
                var key = mKeys[i];
                var buf = i < len ? bufs[0] : bufs[1];
                if (buf.Length > 0)
                    buf.Append('\n');
                var cus = key.customizable;
                if (cus != ECustomizableKey.Customizable)
                {
                    buf.Append("<color=#").
                        Append(cus == ECustomizableKey.SystemBack ? "ff0000" : (cus == ECustomizableKey.SystemStart ? "00ff00" : "00ffff"))
                        .Append(">");
                }
                if (key.isAxis)
                    buf.Append("[A]  ");
                else
                    buf.Append("[B]  ");
                buf.Append(target.name).Append('/').Append(key.keyName);
                if (cus != ECustomizableKey.Customizable)
                    buf.Append("</color>");
                mKeys[i] = key;
            }
            height = Mathf.Max(17, 15 * len);
            keynames[0] = ParallelUtils.ReturnRelease(bufs[0]);
            keynames[1] = ParallelUtils.ReturnRelease(bufs[1]);
            //MainThread.StartLooper(CheckKey);
        }

        private void OnDisable()
        {
            //LooperAction act = CheckKey;
            //MainThread.StopLooper(act);
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            GUI.skin.label.richText = true;
            //EditorGUILayout.BeginVertical("helpbox");
            EditorGUILayout.LabelField("Debug KeyInfos", (GUIStyle)"in titletext");
            EditorGUILayout.Toggle("Is Connected", ((InputController)target).IsConnected);
            EditorGUILayout.BeginHorizontal();
            EditorGUI.BeginDisabledGroup(mDebugKey);
            mDebugKeyName = EditorGUILayout.TextField("Key Name", mDebugKeyName);
            EditorGUI.EndDisabledGroup();
            mDebugKey = GUILayout.Toggle(mDebugKey, mDebugKey ? "Unregist" : "Regist", "button", GUILayout.MaxWidth(80));
            EditorGUILayout.EndHorizontal();

            if (mDebugKey && mAction != null)
            {
                EditorGUI.BeginDisabledGroup(true);
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.FloatField("Action State", mAction.Value);
                var act = mAction.HasButton;
                GUILayout.Toggle(act, act ? "Pressed" : "-", "button", GUILayout.MaxWidth(80));
                EditorGUILayout.EndHorizontal();
                EditorGUI.EndDisabledGroup();
            }

            EditorGUILayout.LabelField("Include KeyInfos", (GUIStyle)"in titletext");
            //EditorGUI.indentLevel++;
            EditorGUILayout.BeginHorizontal();
            GUILayout.Space(15);
            GUILayout.Label(keynames[0], GUILayout.MaxHeight(height));
            GUILayout.Label(keynames[1], GUILayout.MaxHeight(height));
            EditorGUILayout.EndHorizontal();
            //EditorGUI.indentLevel--;
            //EditorGUILayout.EndVertical();
            //Repaint();
            if (mDebugKey)
            {
                if (mAction == null)
                {
                    var i = mDebugKeyName.IndexOf('/');
                    mAction = ((InputController)target).GetAction(i > 0 ? mDebugKeyName.Substring(i + 1) : mDebugKeyName);
                    if (mAction == null)
                        mDebugKey = false;
                }
                else if (mAction is ITick)
                {
                    ((ITick)mAction).OnTick(Time.unscaledDeltaTime);
                }
                Repaint();
            }
            else
            {
                mAction = null;
            }
        }

    }
}